package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.Client;
import cn.wolfcode.wms.mapper.ClientMapper;
import cn.wolfcode.wms.query.QueryObject;
import cn.wolfcode.wms.service.IClientService;
import cn.wolfcode.wms.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClientServiceImpl implements IClientService{

    @Autowired
    private ClientMapper clientMapper;

    public void saveOrUpdate(Client entity) {

        //判断是保存还是添加操作
        if (entity.getId() == null) {
            clientMapper.insert(entity);
        }else{
            clientMapper.updateByPrimaryKey(entity);
        }

    }

   public void delete(Long id) {

        clientMapper.deleteByPrimaryKey(id);
    }

    public Client get(Long id) {

        return clientMapper.selectByPrimaryKey(id);
    }

    public List<Client> selectList() {

        return clientMapper.selectAll();
    }

    public PageResult queryResult(QueryObject qo) {
        Integer rows = clientMapper.queryForCount(qo);
        //判断数据库中的记录数是否为null
        if (rows==0) {
            return PageResult.EMPRY_PAGE;
        }
        //===============
        //如果数据库中有数据
        List<Client> data = clientMapper.queryForPage(qo);

        return new PageResult(data,rows,qo.getCurrentPage(),qo.getPageSize());
    }
}
